'''
Created on Oct 8, 2012

@author: Himanshu
'''
import os
import cPickle as pickle
import matplotlib.pyplot as mplt


class ReSampler():
    
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        
    def meshSample(self, timeStamps, featureVector, meshSize):
        "Make sure equal length vectors passed in"
        if featureVector.__len__() != timeStamps.__len__():
            print "Unequal length vectors. Meshing unsuccessful" 
            return 
        origSize = featureVector.__len__()
        endTime = timeStamps[origSize - 1]
        startTime = timeStamps[0]
        newSize = 1 + ((endTime - startTime)/meshSize)
        oldIndex = 0
        print 'Original Size: ' , origSize
        print 'New Size: ' , newSize
        newSize = int(newSize)
        "Sample"
        newTimeStamps = [0]*newSize
        newFeatures = [0]*newSize
        for i in range(0,newSize):
            time_i = i*meshSize + startTime
            newTimeStamps[i] = time_i
            if (time_i > timeStamps[oldIndex + 1]):
                oldIndex += 1
            newFeatures[i] = self.interpolate(timeStamps[oldIndex], featureVector[oldIndex], timeStamps[oldIndex+1],
                                          featureVector[oldIndex + 1], time_i)
        return (newTimeStamps, newFeatures)
    
    def interpolate(self,startTime, startVal, endTime, endVal, queryTime):
        slope = (endVal - startVal)/(endTime - startTime)
        return startVal + slope * (queryTime - startTime)
               
    def reSampleAll(self, meshSize):
        path = os.getcwd() + "\data"
        for fileName in os.listdir(path):
            if (os.path.splitext(fileName)[1] == ".pkl"):
                pklFile = open(path + '\\' + fileName, 'rb')
                gestureName = pickle.load(pklFile)
                #print gestureName
                noGestures = pickle.load(pklFile)
                #print noGestures , ' noGestures'
                tup1 = pickle.load(pklFile)
                pklFile.close()
                timeStampLists = tup1[1]
                featureLists = tup1[0]
                """print featureLists.__len__() , ' featureList length'
                print timeStampLists.__len__() , ' timeStampList length'"""
                newTimeStampLists = []
                newFeatureLists = []
                for i in range(0, noGestures - 1):
                    tup2 = self.meshSample(timeStampLists[i], featureLists[i], meshSize)
                    mplt.Figure
                    mplt.plot(timeStampLists[i], featureLists[i], 'r+', markersize = 12)
                    mplt.hold(True)
                    newTimeStampLists.append(tup2[0])
                    newFeatureLists.append(tup2[1])
                    mplt.plot(tup2[0],tup2[1], linewidth = 2)
                    #mplt.show()
                """print ' ----------- OUTPUT ----------'
                print newFeatureLists.__len__() , ' new FeatureLists length'
                print newTimeStampLists.__len__() , 'new newTimeStampLists length'
                """
                output = open(path + '\\' + os.path.splitext(fileName)[0] + '_re.pkl', 'wb')
                pickle.dump(gestureName,output)
                pickle.dump(noGestures, output)
                pickle.dump((newTimeStampLists,newFeatureLists), output)
                

                
                    
                
        
        
        